package com.itheima.setlist;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SetList {
    public static void main(String[] args) {
        Set<Student> s = new TreeSet<>();
        s.add(new Student("定定",18));
        s.add(new Student("宝宝定",17));
        s.add(new Student("定宝",16));
        s.add(new Student("定宝宝",18));

        Iterator<Student> it = s.iterator();
        while(it.hasNext()){
            Student next = it.next();
            System.out.println(next);
        }


    }
}
class Student implements Comparable<Student>{
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
       /* int result =  this.age-o.age;
        result = result == 0 ? this.name.compareTo(o.name) : result;
        return result;*/
       int result = o.age - this.age;
       result = result == 0 ? o.name.compareTo(this.name) : result;
       return result;
    }
}